//1
package TUI;

import Ctr.ItemCtr;
import Model.Item;
import java.util.Scanner;
/**
 *
 */
public class ItemUI
{
    private ItemCtr itemCtr;
    private UpdateItemUI updateItemUI;
    private InputUI inputUI;

    /**
     *
     */
    public ItemUI()
    {
        itemCtr = new ItemCtr();
        updateItemUI = new UpdateItemUI();
        inputUI = new InputUI();
    }

    /**
     *
     */
    public void start()
    {

        itemMenu();
    }

    /**
     *
     */
    public void itemMenu()
    {
        try
        {
            boolean exit = false;
            while(!exit)
            {
                //show menu and return the user's choice
                int choice =   writeItemMenu();
                switch(choice)
                {
                    case 0:
                    exit = true;
                    break;
                    case 1:
                    getItem();
                    break;
                    case 2:
                    createItem();
                    break;
                    case 3:
                    startUpdateMenuUI();
                    break;
                    case 4:
                    deleteItem();
                    break;
                    case 5:
                    listAllItems();
                    break;

                }
            }
        }
        catch(Exception e)
        {
            start();
        }
    }

    /**
     * 
     * @return
     */

    private int writeItemMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Item Menu ***");
        System.out.println(" (1) Find item");
        System.out.println(" (2) Create item");
        System.out.println(" (3) Update product");
        System.out.println(" (4) Delete item");
        System.out.println(" (5) Display all items");
        System.out.println(" (0) Back");
        System.out.print("\n Write choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    /**
     * 
     */
    private void getItem()
    {
        int itemID = inputUI.inputID();
        //is to be changed when the DVD controller is completed
        if(itemCtr.getItem(itemID) != null)
        {
            System.out.println ("Product Name:" + itemCtr.getItem (itemID).getItemName());
            System.out.println ("Price:" + itemCtr.getItem(itemID).getItemPrice());
            System.out.println ("Description:" + itemCtr.getItem(itemID).getDescription());
            System.out.println ("Maximum Stock:" + itemCtr.getItem(itemID).getMaxInStock());
            System.out.println ("Minimum Stock" + itemCtr.getItem(itemID).getMinInStock());
            System.out.println ("Number:" + itemCtr.getItem (itemID).getItemsInStock());
            System.out.println ("Company:" + itemCtr.getItem(itemID).getBrand());
        }
        else
        {
            System.out.println ("ID not found");     
        }
        inputUI.pause();
    }

    /**
     * 
     */
    private void createItem()
    {

        String itemName = inputUI.inputItemName();
        double itemPrice = inputUI.inputItemPrice();
        String description = inputUI.inputDescription();        
        int minInStock = inputUI.inputMinInStock();
        int maxInStock = inputUI.inputMaxInStock();
        while(maxInStock <= minInStock)
        {
            System.out.println("Maksmimums stock is less than or equal to the minimum stock");
            maxInStock = inputUI.inputMaxInStock();
        }
        int itemsInStock = inputUI.inputItemsInStock();

        while(itemsInStock > maxInStock || itemsInStock < minInStock)
        {
            System.out.println("The number of copies must be within the minimum / maximum stock");
            itemsInStock = inputUI.inputItemsInStock();
        }
        boolean isYes = inputUI.UnitYesOrNo();
        String brand = inputUI.inputBrand();
        String place = inputUI.inputPlace();
        int i = itemCtr.createItem(itemName, itemPrice, description, maxInStock, minInStock, itemsInStock, brand, place, isYes);
        System.out.println("The product is created and has been given the ID" + i);
        inputUI.pause();
    }

    /**
     * 
     */

    private void deleteItem()
    {
        int itemID = inputUI.inputItemID();
        if(itemCtr.getItem(itemID) == null)
        {
            System.out.println("ID" + itemID + "does not exist. Please enter a new ID.");
        }
        else
        {
            boolean yesOrNo = inputUI.yesOrNo();
            if(yesOrNo == true)
            {
                itemCtr.deleteItem(itemID);
                System.out.println("The product with the ID" + itemID + "was deleted.");
            }
        }
    }

    /**
     * 
     */

    private void listAllItems()
    {
        for(Item i : itemCtr.getItemList())
        {
            System.out.println("ItemID: " + i.getItemID() + " Item Name: " +
                i.getItemName() + " Price: " + i.getItemPrice() + 
                " Antal: " + i.getItemsInStock() + " Company: " +
                i.getBrand() + " Location: " + i.getPlace());
        }
        inputUI.pause();
    }

    /**
     *
     */
    public void startUpdateMenuUI()
    {
        updateItemUI.start();
    }
}